<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /**
     * 给出一段 html 元素字符串：
     * <script src="//storage.360buyimg.com/retail-mall/lib/sha256-new.js?v=2024-06-20-17" data-s="security"><\/script>
     *
     * 根据其中的自定义属性 data-s="security" 将该元素在 html 中整体替换成 {security}
     * */

    const htmlStr = '<script src="//storage.360buyimg.com/retail-mall/lib/sha256-new.js?v=2024-06-20-17" data-s="security"><\/script>' +
        '<script src="//storage.360buyimg.com/retail-mall/lib/sha256-new.js?v=2024-06-20-18"><\/script>' +
        '<script data-s="ffff">var a= 2<\/script>'

    const linkStr= `<link href="//jdcstatic.360buyimg.com/jdcdkh/bjd-common/bhome/purchaselist/1.0.1/bjd.css" rel="stylesheet" data-s="purchaselist" />
                    <link href="//jdcstatic.360buyimg.com/jdcdkh/bjd-common/bhome/purchaselist/1.0.1/bjd.css" rel="stylesheet" />
                    <link href="//jdcstatic.360buyimg.com/jdcdkh/bjd-common/bhome/purchaselist/1.0.1/bjd.css" rel="stylesheet" data-s="purchaselist2" />`

    // 目前就替换 script 和 link 这两中标签
    function replaceTpl (str, type) {
        if (type === 'script') {
            // console.log(str.match(/<script\b[^>]*data-s="(.*?)"[^>]*>([\s\S]*?)<\/script>/g))
            const newStr = str.replace(/<script\b[^>]*data-s="([^"|']*)"[^>]*>([\s\S]*?)<\/script>/g, function () {
                // console.log(arguments)
                return `{${arguments[1]}}`
            })
            console.log(newStr)
            return newStr
        }
        if (type === 'link') {
            const newStr = str.replace(/<link\b[^>]*data-s="([^"|']*)"[^>]*\/>/g, function () {
                // console.log(arguments)
                return `{${arguments[1]}}`
            })
            console.log(newStr)
            return newStr
        }
    }
    // replaceTpl(htmlStr, 'script')
    replaceTpl(linkStr, 'link')
</script>
</body>
</html>